<?php

namespace App\Containers\AppSection\Settings\UI\API\Requests;

use App\Containers\AppSection\Settings\Tasks\FindSettingByKeyTask;
use App\Ship\Parents\Requests\Request;
use Auth;

class GetSettingRequest extends Request
{
    /**
     * Define which Roles and/or Permissions has access to this request.
     */
    protected array $access = [
        'permissions' => '',
        'roles' => '',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     */
    protected array $decode = [
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     */
    protected array $urlParameters = [];

    public function rules(): array
    {
        return [];
    }

    public function isPublic()
    {
        $setting = app(FindSettingByKeyTask::class)->run($this->key);
        if (!Auth::user()->hasAdminRole()) {
            return $setting->public;
        }
        return true;
    }

    public function authorize(): bool
    {
        return $this->check([
            'hasAccess',
            'isPublic'
        ]);
    }
}
